import { Enum } from "@/js/enum";
import { Merchant } from '@/js/api/api_merchant'
import { Channel } from "@/js/api/api_channel"
import { Combo } from "@/js/api/api_combo";
import { Foundation } from "@/js/api/api_foundation";
import { Card } from "@/js/api/api_card";
import { Task } from "@/js/api/api_task";
import { Order } from "@/js/api/api_order";
import { Status } from "@/js/api/api_status";

// 对外公布基础类
class Api { }

// 枚举挂载
Api.prototype.Enum = Enum
Api.prototype.merchant = new Merchant()
Api.prototype.channel = new Channel()
Api.prototype.combo = new Combo()
Api.prototype.foundation = new Foundation()
Api.prototype.card = new Card()
Api.prototype.task = new Task()
Api.prototype.order = new Order()
Api.prototype.buttonPower = function (param) {
  let routerPower = JSON.parse(localStorage.getItem('routerPower'))
  let v = false
  routerPower.forEach(element => {
    if (element.id == param) {
      v = true
    }
  })
  return v
}
Api.prototype.status = new Status()
// 废弃 建议使用 forbidHeader 方法
Api.prototype.power = (queryItem = [], tableItem = [], callback) => {
  let power = JSON.parse(localStorage.getItem('userData')).forbid_fields
  power.forEach(element => {
    tableItem.forEach((item, index) => {
      if (item.prop == element) {
        tableItem.splice(index, 1)
      }
    })
    queryItem.forEach((item, index) => {
      if (element == item.model) {
        queryItem.splice(index, 1)
      }
    })
  })
  callback({ tableItem: tableItem, queryItem: queryItem })
}
// 禁止 header
//  元素需包含 key 字段
Api.prototype.forbidHeader = function (... hederGroup){
  let power = JSON.parse(localStorage.getItem('userData')).forbid_fields
  power.forEach(e =>{
    hederGroup.forEach(group=>{
      group.forEach((head, index)=>{
        if (head.key == e){
          group.splice(index, 1)
        }
      })
    })
  })
}

const api = new Api()
export default api